Skip to content

use code_blockt::add and ::move for adding a statement #3067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2018

Conversation

kroening
Copy link
Member

This is the trivial part of #2830.

  • Each commit message has a non-empty body, explaining why the change was made.
  • My contribution is formatted in line with CODING_STANDARD.md.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@@ -2744,7 +2744,7 @@ codet java_bytecode_convert_methodt::convert_iinc(
arg1_int_type.make_typecast(java_int_type());
code_assign.rhs() =
plus_exprt(variable(arg0, 'i', address, CAST_AS_NEEDED), arg1_int_type);
block.copy_to_operands(code_assign);
block.add(code_assign);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -215,13 +215,13 @@ exprt allocate_dynamic_object(
symbols_created.push_back(&malloc_sym);
code_assignt assign=code_assignt(malloc_sym.symbol_expr(), malloc_expr);
assign.add_source_location()=loc;
output_code.copy_to_operands(assign);
output_code.add(assign);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

exprt malloc_symbol_expr=malloc_sym.symbol_expr();
if(cast_needed)
malloc_symbol_expr=typecast_exprt(malloc_symbol_expr, target_expr.type());
code_assignt code(target_expr, malloc_symbol_expr);
code.add_source_location()=loc;
output_code.copy_to_operands(code);
output_code.add(code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -230,7 +230,7 @@ exprt allocate_dynamic_object(
null_pointer_exprt null_pointer_expr(to_pointer_type(target_expr.type()));
code_assignt code(target_expr, null_pointer_expr);
code.add_source_location()=loc;
output_code.copy_to_operands(code);
output_code.add(code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -320,7 +320,7 @@ exprt java_object_factoryt::allocate_object(
aoe=typecast_exprt(aoe, target_expr.type());
code_assignt code(target_expr, aoe);
code.add_source_location()=loc;
assignments.copy_to_operands(code);
assignments.add(code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -1237,7 +1235,7 @@ void java_object_factoryt::gen_nondet_init(
code_assignt assign(expr, rhs);
assign.add_source_location()=loc;

assignments.copy_to_operands(assign);
assignments.add(assign);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -1408,13 +1406,13 @@ void java_object_factoryt::gen_nondet_array_init(
exprt counter_expr=counter.symbol_expr();

exprt java_zero=from_integer(0, java_int_type());
assignments.copy_to_operands(code_assignt(counter_expr, java_zero));
assignments.add(code_assignt(counter_expr, java_zero));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -1393,7 +1391,7 @@ void java_object_factoryt::gen_nondet_array_init(
const auto &array_init_symexpr=array_init_symbol.symbol_expr();
codet data_assign=code_assignt(array_init_symexpr, init_array_expr);
data_assign.add_source_location()=loc;
assignments.copy_to_operands(data_assign);
assignments.add(data_assign);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -1328,7 +1326,7 @@ void java_object_factoryt::allocate_nondet_length_array(
java_new_array.type().subtype().set(ID_element_type, element_type);
code_assignt assign(lhs, java_new_array);
assign.add_source_location() = loc;
assignments.copy_to_operands(assign);
assignments.add(assign);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

@@ -170,7 +170,7 @@ void java_simple_method_stubst::create_method_stub(symbolt &symbol)
code_assignt get_argument(
init_symbol_expression, symbol_exprt(this_argument.get_identifier()));
get_argument.add_source_location() = synthesized_source_location;
new_instructions.copy_to_operands(get_argument);
new_instructions.add(get_argument);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move?

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 29bd494).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86391063

@@ -141,7 +141,7 @@ static void monitor_exits(codet &code, const codet &monitorexit)
// Replace the return with a monitor exit plus return block
code_blockt return_block;
return_block.add(monitorexit);
return_block.move_to_operands(code);
return_block.move(code);
code = return_block;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about code = code_blockt({monitorexit, code});

try_block.move(catch_push);
try_block.move(code);
try_block.move(catch_pop);
try_block.move(catch_label);
code = try_block;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code = code_blockt({monitorenter, catch_push, code, catch_pop, catch_label});

@@ -856,7 +856,7 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table)
code_blockt clone_body;

code_declt declare_cloned(local_symexpr);
clone_body.move_to_operands(declare_cloned);
clone_body.move(declare_cloned);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the declaration of clone_body further down and do code_blockt clone_body({declare_cloned, create_blank, declare_index, copy_loop, return_inst}); (if I got this right from what I see in the code review).

ret_block.move_to_operands(clinit_call);
ret_block.move_to_operands(c);
ret_block.move(clinit_call);
ret_block.move(c);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt ret_block({clinit_call, c});

ret_block.move_to_operands(assert_code);
ret_block.move_to_operands(assume_code);
ret_block.move(assert_code);
ret_block.move(assume_code);
c = ret_block;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt ret_block({assert_code, assume_code}); (and other places use std::move afterwards).

@@ -163,7 +163,8 @@ void cpp_typecheckt::typecheck_switch(code_switcht &code)
c_typecheck_baset::typecheck_switch(code);

code_blockt code_block;
code_block.move_to_operands(decl.op0(), code);
code_block.move(to_code(decl.op0()));
code_block.move(code);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt code_block({to_code(decl.op0()), code});

@@ -1748,7 +1748,7 @@ void goto_program2codet::cleanup_code_ifthenelse(
// we re-introduce 1-code blocks with if-then-else to avoid dangling-else
// ambiguity
code_blockt b;
b.move_to_operands(i_t_e.then_case());
b.move(i_t_e.then_case());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt b({i_t_e.then_case()});

@@ -1760,7 +1760,7 @@ void goto_program2codet::cleanup_code_ifthenelse(
// we re-introduce 1-code blocks with if-then-else to avoid dangling-else
// ambiguity
code_blockt b;
b.move_to_operands(i_t_e.else_case());
b.move(i_t_e.else_case());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt b({i_t_e.else_case()});

b.move_to_operands(else_label);
b.move(i_t_e);
b.move(then_label);
b.move(else_label);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt b({i_t_e, then_label, else_label});

result.copy_to_operands(code_assertt(condition));
result.copy_to_operands(code_assumet(condition));
result.add(code_assertt(condition));
result.add(code_assumet(condition));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code_blockt result({code_assertt(condition), code_assumet(condition)});

@kroening
Copy link
Member Author

@tautschnig @peterschrammel Please note that your suggestions to into opposite directions: Michael's means more copying, Peter wants less.

@tautschnig
Copy link
Collaborator

Please note that your suggestions to into opposite directions: Michael's means more copying, Peter wants less.

Well, yes, but on different bits of the code base. @peterschrammel's suggestions exclusively cover cases that require (at least so it seems) incremental construction; mine talks about cases where likely we could even add a const as the object is fully constructed in just one line of code.

@kroening
Copy link
Member Author

kroening commented Oct 1, 2018

Perhaps to make more fundamental progress on this:

  for(int i=0; i<2000000; i++)
  {
    irep_idt id;
    symbol_exprt s(id, bool_typet());
    exprt something;
    something.move_to_operands(s);
    //something.copy_to_operands(s);
  }

with move_to_operands: 0.59s
with copy_to_operands: 0.55s

This does not appear to require a recent compiler. The difference is actually difficult to measure (it's in the range of the noise). Thus, there is absolutely no reason to believe that moving ireps would be any faster than copying.

The key reason why the moving methods exist is that they did offer enormous benefit before the sharing was introduced. I am thus tempted to remove the .move() method, say for code_blockt as a starter. It's simply a distraction.

@tautschnig
Copy link
Collaborator

@kroening Thank you for doing this measurement! Radical as I am, may I propose the even bigger step of getting rid of move_to_operands altogether (in the long term, not in this PR)? And then we could also inline copy_to_operands...

@kroening kroening force-pushed the code_block_move_add_partial branch from 29bd494 to 0a16bb6 Compare October 1, 2018 11:20
@kroening
Copy link
Member Author

kroening commented Oct 1, 2018

Done; please take another look.

@kroening kroening assigned tautschnig and peterschrammel and unassigned kroening Oct 1, 2018
@kroening
Copy link
Member Author

kroening commented Oct 1, 2018

@tautschnig Indeed, will do a PR that deprecates move_to_operands().

@smowton
Copy link
Contributor

smowton commented Oct 1, 2018

Hang on, the move operations is surely only similar like this when there is no usage of the moved irept subsequently. For example,

irept i1 = something();
irept copy = i1;
copy.id() = "hello world";

This will cause a CoW break upon calling the non-const variant of .id(), whereas if we had moved i1 there would be no break. That sort of situation would surely produce a significant difference.

In the particular case discussed here we would have something like

block.move_to_operands(something);
...
replace_symbol(block, ...);

If something was copied then replace_symbol may cause a CoW break, whereas if it was moved then it won't.

@kroening
Copy link
Member Author

kroening commented Oct 1, 2018

@smowton Indeed, this assumes that the moved object isn't touched afterwards; in your example, I'd assume that 'something' isn't ever used after the move_to_operands. I would assume that to be the case across the code base, and would consider anything else to be a bug.

@smowton
Copy link
Contributor

smowton commented Oct 1, 2018

Right, but operations against the destination of the move are allowed, and if they take place before the moved-from handle has died then a needless CoW break will occur.

@smowton
Copy link
Contributor

smowton commented Oct 1, 2018

(in both my examples it's the target that's being altered, and the source handle just happens to still be alive)

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 234ac62).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86463616

tautschnig added a commit to tautschnig/cbmc that referenced this pull request Oct 1, 2018
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of
incrementally constructing objects do RAII.
tautschnig added a commit to tautschnig/cbmc that referenced this pull request Oct 1, 2018
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of
incrementally constructing objects do RAII.
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend using std::initializer_list, and as previously mentioned, object to move -> add in general.

return_block.add(monitorexit);
return_block.move_to_operands(code);
code = return_block;
code = code_blockt({monitorexit, code});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going for this style, could I recommend replacing the constructor for code_blockt that takes a std::list<const codet &> with one taking a std::initializer_list? The former means a bunch of heap allocates and deallocates, whereas the latter is basically a type-safe layer atop varargs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(It could also take universal references, permitting {monitorexit, std::move(code)})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Correction, it's not a wrapper on <stdarg> style varargs as I'd thought, but it is a fixed-size array, probably stack-allocated, which still avoids much of the pointless back-and-forth of a temporary linked list)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(You could also use a variadic template... but that's probably more fuss than it's worth when the initializers are monovariant)

Copy link
Member Author

@kroening kroening Oct 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, there is unnecessary copying going on in the constructor. How about these:

  explicit code_blockt(const std::vector<codet> &_statements):codet(ID_block)
  {
    operands()=(const std::vector<exprt> &)_statements;
  }

  explicit code_blockt(std::vector<codet> &&_statements):codet(ID_block)
  {
    operands()=std::move((std::vector<exprt> &&)_statements);
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not std::initializer_list<codet>? Should be just like vector, except without the heap alloc; plus it's (one of the) new standard ways to do varargs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that add anything over offering the vector interface? Note that the vector offers a constructor from std::initializer_list, and that the vector needs to exist eventually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think marginally, but the difference is small so let's go with this. It's definitely much better than std::list for this purpose.

tautschnig added a commit to tautschnig/cbmc that referenced this pull request Oct 1, 2018
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of
incrementally constructing objects do RAII.
@kroening kroening force-pushed the code_block_move_add_partial branch from 234ac62 to 8c40aa1 Compare October 2, 2018 09:41
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 8c40aa1).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86610869

tautschnig added a commit to tautschnig/cbmc that referenced this pull request Oct 2, 2018
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of
incrementally constructing objects do RAII.
@kroening
Copy link
Member Author

kroening commented Oct 3, 2018

@smowton To make progress on this, how about a variant of code_blockt::add that gets an rvalue reference? You'd then write

block.add(std::move(something));

in the scenario in which you would modify the added statement after adding.

@smowton
Copy link
Contributor

smowton commented Oct 3, 2018

Sure? Don't mind re: block.move(codet) and block.add(codet &&), I just don't want to needlessly throw away a useful opportunity to reduce CoW breaks

@kroening kroening force-pushed the code_block_move_add_partial branch from 8c40aa1 to 28beb76 Compare October 3, 2018 10:10
@kroening
Copy link
Member Author

kroening commented Oct 3, 2018

Ok, done!

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 28beb76).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86726683

Copy link
Member

@peterschrammel peterschrammel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Daniel Kroening added 4 commits October 7, 2018 17:29
The new constructors avoid building a vector from a list.  The vector is
constructed directly at the call site of the constructor.
.move_operands() is avoided; there is no measurable performance benefit.
@kroening kroening force-pushed the code_block_move_add_partial branch from 28beb76 to 46232f0 Compare October 7, 2018 17:27
@kroening kroening merged commit 8ecbec9 into develop Oct 7, 2018
@kroening kroening deleted the code_block_move_add_partial branch October 7, 2018 19:01
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed Diffblue compatibility checks (cbmc commit: 46232f0).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/87134284

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants